package com.merry.boot.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class HelloController {

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }


    @GetMapping("/{pathId}/testParam")
    @ResponseBody
    public Map<String,String> testParam(
            @PathVariable("pathId") String pathId,
            @RequestParam(value = "paramId",required = false) String paramId,
            @RequestBody(required = false) String bodyId
    ){
        Map<String,String> map=new HashMap<>();
        map.put("pathId",pathId);
        map.put("paramId",paramId);
        map.put("bodyId",bodyId);
        return null;
    }


    @GetMapping("/testParam1")
    public String testParam1(Map<String,String> map,
                             Model model,
                             HttpServletRequest request,
                             HttpServletResponse response){
        map.put("hello","1");
        model.addAttribute("model","model1");
        request.setAttribute("request","request1");
        response.addCookie(new Cookie("cookie","1"));
        return "forward:/success";
    }


    /**
     * 在map、model中添加的数据 == request.setAttribute
     * @param request
     * @return
     */
    @GetMapping("/success")
    @ResponseBody
    public Map success(HttpServletRequest request){
        Map<String,String> map =new HashMap<>();

        Object hello = request.getAttribute("hello");
        Object model = request.getAttribute("model");
        Object request1 = request.getAttribute("request");

        map.put("hello", (String) hello);
        map.put("model", (String) model);
        map.put("request", (String) request1);

        return map;
    }

}
